/*
 * ProjetoPSA - Plano de Substituição de Aulas
 *    Copyright (C) 2010  Marcelo e Cicero
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projetoPSA.facades;



import java.util.List;
import java.util.logging.Logger;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Expression;
import projetoPSA.hibernate.DaoGenerico;
import projetoPSA.pojos.*;

/**
 *
 * @author Marcelo e Cicero
 */
public class GradeFacade {

    private Grade grade;
    private Session sessao;
    private Criteria criteria;
    private Logger log = Logger.getLogger(this.getClass().getName());

    public GradeFacade(Grade grade) {
        this.grade = grade;
    }

    public GradeFacade() {
        begin();
    }


    private void begin() {
        this.sessao = new DaoGenerico().getSessao();
        this.criteria = sessao.createCriteria(Grade.class);
        log.info("Iniciando Sessão!");
    }

    private void commit() {
        this.sessao.beginTransaction().commit();
        log.info("Efetuando commit...");
        this.sessao.flush();
        this.sessao.close();
        log.info("Fechando sessao!");
    }


    public Grade getGrade(Long id){
        begin();
        Grade ret = (Grade) criteria.add(Expression.eq("id", id)).uniqueResult();
        commit();
        return ret;
    }

    public List<Grade>getGrades(){
        begin();
        List<Grade> ret = criteria.list();
        commit();
        return ret;
    }

    public List<Grade>getGrades(Professor professor){
        begin();
        List ret = criteria.add(Expression.eq("professor", professor)).list();
        commit();
        return ret;
    }
    public List<Grade>getGrades(Turma turma){
        begin();
        List ret = criteria.add(Expression.eq("turma", turma)).list();
        commit();
        return ret;
    }
    public List<Grade>getGrades(DiaSemana diaSemana){
        begin();
        List ret = criteria.add(Expression.eq("diaSemana", diaSemana)).list();
        commit();
        return ret;
    }

    
    public void addProfessor(Professor professor) {
        this.grade.setProfessor(professor);
        professor.setGrade(grade);
    }

    public void addDisciplina(Disciplina disciplina) {
        this.grade.setDisciplina(disciplina);
        disciplina.setGrade(grade);
    }

    public void addDiaSemana(DiaSemana diaSemana) {
        this.grade.setDiaSemana(diaSemana);
        diaSemana.setGrade(grade);
    }

    public void addSala(Sala sala) {
        this.grade.setSala(sala);
        sala.setGrade(grade);
    }

    public void addTurma(Turma turma) {
        this.grade.setTurma(turma);
        turma.setGrade(grade);
    }

    public void addHorario(Horario horario) {
        this.grade.setHorario(horario);
        horario.setGrade(grade);
    }

    public void persiste() {
        begin();
        sessao.saveOrUpdate(grade);
        commit();
    }

    public void delete() {
        begin();
        sessao.delete(grade);
        commit();
    }
}
